package com.bnz.controller;

import com.bnz.pojo.*;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    //1. 所有学生
    @GetMapping("list")
    public String findAll(Model model){
        List<Student> all = studentService.findAll();
        model.addAttribute("list",all);
        return "student/list";
    }

    //2. 分页查询
    @RequestMapping("findByPage")
    public String findByPage(@RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "3") int pageSize,
                             Model model){
        PageResult<Student> pr = studentService.findByPage(page,pageSize);
        model.addAttribute("pr",pr);
        return "student/list";
    }

    // 条件查询
    @RequestMapping("search")
    public String search(@RequestParam(required = false,defaultValue = "1") int page,
                         @RequestParam(required = false,defaultValue = "3") int pageSize,
                         StudentVo vo,
                         Model model){
        PageResult<Student> pr = studentService.search(page,pageSize,vo);
        List<Classes> classes = classesService.findAll();
        model.addAttribute("pr",pr);
        model.addAttribute("classes",classes);
        model.addAttribute("stud",vo);
        return "student/list";
    }

    // 添加学生
    @PostMapping("add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:/student/search";
    }

    // 修改学生
    @PostMapping("update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:/student/search";
    }

    //删除学生
    @GetMapping("delete/{id}")
    public String delete(@PathVariable String id){
        studentService.delete(id);
        return "redirect:/student/search";
    }
}
